#!/bin/sh

# Use to start the ESpace Server (ESS). The ESpace server should
# only be run on the gateway machine, not on client machines.
#
# NOTE: It should not be necessary to restart the ESS. An entry is
#       placed into /etc/inittab (entry SvcAgentESS) when Service
#       Agent is installed which will automatically restart it. If
#       it stops running the reason for this should be investigated
#       and any problems rectified.

abortess()
{
  cd /var/svcagent
  if [ -f "locks/lock.ess" ]
  then
    a=locks/lock.ess
    essid=`cat $a`
    if [ -n $essid ]
    then
      kill $essid
    fi
  fi
}

# Ensure that user svcagent runs this script to prevent security risks
id=`id`
id=${id#*\(}
id=${id%%\)*}
if [ "$id" != "svcagent" ]
then
  echo "This script must be run by the user svcagent."
  exit 1
fi

op=$1
noa=$#
if [ $noa -eq 1 ]
then
  if  [ "$op" == "abort" ] || [ "$op" == "-abort" ]
  then
    abortess
    exit
  fi
fi

export CLASSPATH=$CLASSPATH:/usr/svcagent/lib/ibm.jar:/usr/svcagent/lib/swingall.jar:/usr/svcagent/lib/collections.jar:/usr/svcagent/lib/sahelp.jar:/usr/svcagent/lib/sai18n.jar:/usr/svcagent/lib/salists.jar

# Modified IBM Sai.
# Check if the disk space is sufficient to run Service agent
img=`ls -al /var/svcagent/saspace/saspace.img | awk '{print $5}'`
jrl=`ls -al /var/svcagent/saspace/saspace.jrl | awk '{print $5}'`
avail=`df -k /var | awk '{print $4}' | grep -v Available`
let tot=$img+$jrl+10240
let cs=$tot/1024
echo " CS = " $cs "  " `expr cs>avail`  "  AVAIL = " $avail `expr cs<avail` 
#if [ `expr "$avail" < "$cs"` ]
if test $cs -gt $avail
then
  echo "Insufficient disk space in /var to Run Service Agent Application."
  echo "Insufficient disk space in /var to Run the Service Agent Application.. " >>/dev/console
  # Log this information into ess.stat file.
  echo "Insufficient disk space in /var to Run the Service Agent Application.. " >>/var/svcagent/logs/stat.ess
  echo "REQUIRED disk space : " $cs " K" >>/var/svcagent/logs/stat.ess
  echo "AVAILABLE disk space : " $avail " K" >>/var/svcagent/logs/stat.ess
  echo "*****" >>/var/svcagent/logs/stat.ess
  # Send a mail to the root
  exit
fi

#  Modified IBM Sai. Until here

cd /var/svcagent
#while (( 1 ))
while ( `expr 1 > 0` )
do
  let count=5
  echo  COUNT = $count 
  while ( `expr $count > 0` )
  do
    echo " In the Loop "
    [ -d locks ] || mkdir locks
    mkdir locks/lock  # 2>/dev/null
    if [ $? -eq 0 ]
    then
      if [ -f "locks/lock.ess" ]
      then
        echo "lock.ess Pesent"
        a=locks/lock.ess
        id=`cat $a`
        p=`ps -ef --cols 400| egrep "^ *svcagent +$id .+ com.ibm.ecf.apps.ESS"|grep -v grep|awk '{print $2}' 2>/dev/null`
        #p=`ps wax | egrep "^ * +$id .+ com/ibm/ecf/apps/ESS"` #2>/dev/null
        #p=`ps wax | egrep "$id .+ com/ibm/ecf/apps/ESS"|grep -v grep`
        echo "ESS PID = " $p 
        if [ -n "$p" ] 
        then
          rmdir locks/lock
          echo "ESS already running."
          exit
        fi
      fi
      export DISPLAY=display.display.display.ibm   
      echo $DISPLAY
      echo $CLASSPATH
      bc=""
      bc=`java -Djava.security.policy=svcagent.policy com.ibm.ecf.apps.Bcp`
      #nohup java $bc -Dmx64M -Djava.security.policy=svcagent.policy com.ibm.ecf.apps.ESS >/var/svcagent/logs/log.ess 2>&1 &
      nohup java $bc -Dmx64M -Djava.security.policy=svcagent.policy com.ibm.ecf.apps.ESS >/dev/null 2>&1 &
      PID=$!
      echo $PID > locks/lock.ess
      rmdir locks/lock
      echo "ESS Started."
      #echo "ESS Started : `date`" >> /var/svcagent/logs/stat.ess
      #echo "ESS Started : `date`" >> /var/svcagent/logs/log.ess

      # Note: this script is respawned from /etc/inittab. This means
      # need to keep this process alive so that multiple copies
      # of the ESS are not started.
      wait $PID
      #exit
    else 
      let count=`expr $count - 1`
      echo "$0: Waiting for lock permission ... $count"
    fi
    sleep 15
  done
  echo "Out of the Loop" $count
  if [ $count -eq 0 ]
  then
    echo "Lock directory locked"
    rmdir locks/lock
    #exit 1
  fi
done
echo "End OF File " 
